/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.pub.web;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.pub.entity.PubAffect;
import com.thinkgem.jeesite.modules.pub.entity.PubHelp;
import com.thinkgem.jeesite.modules.pub.service.PubHelpService;

/**
 * 单表生成Controller
 * @author zmy
 * @version 2019-04-18
 */
@Controller
@RequestMapping(value = "${adminPath}/pub/pubHelp")
public class PubHelpController extends BaseController {

	@Autowired
	private PubHelpService pubHelpService;
	
	@ModelAttribute
	public PubHelp get(@RequestParam(required=false) String id) {
		PubHelp entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = pubHelpService.get(id);
		}
		if (entity == null){
			entity = new PubHelp();
		}
		return entity;
	}
	
	@RequiresPermissions("pub:pubHelp:view")
	@RequestMapping(value = {"list", ""})
	public String list(PubHelp pubHelp, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<PubHelp> page = pubHelpService.findPage(new Page<PubHelp>(request, response), pubHelp); 
		model.addAttribute("page", page);
		return "modules/pub/pubHelpList";
	}

	@RequiresPermissions("pub:pubHelp:view")
	@RequestMapping(value = "form")
	public String form(PubHelp pubHelp, Model model) {
		model.addAttribute("pubHelp", pubHelp);
		return "modules/pub/pubHelpForm";
	}

	@RequiresPermissions("pub:pubHelp:edit")
	@RequestMapping(value = "save")
	public String save(PubHelp pubHelp, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, pubHelp)){
			return form(pubHelp, model);
		}
		pubHelpService.save(pubHelp);
		addMessage(redirectAttributes, "保存求助专栏成功");
		return "redirect:"+Global.getAdminPath()+"/pub/pubHelp/?repage";
	}
	
	@RequiresPermissions("pub:pubHelp:edit")
	@RequestMapping(value = "delete")
	public String delete(PubHelp pubHelp, RedirectAttributes redirectAttributes) {
		pubHelpService.delete(pubHelp);
		addMessage(redirectAttributes, "删除求助专栏成功");
		return "redirect:"+Global.getAdminPath()+"/pub/pubHelp/?repage";
	}
	
	@RequestMapping(value="wxlist")
	public String wxlist(PubHelp pubHelp, HttpServletRequest request, HttpServletResponse response, Model model) {
		List<Object[]> page = pubHelpService.helpList(); 
//		model.addAttribute("page", page);
		return renderString(response, page);
	}
	@RequestMapping(value="wxget")
	public String wxget(HttpServletRequest request, HttpServletResponse response,String id) {
		PubHelp entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = pubHelpService.get(id);
		}
		if (entity == null){
			entity = new PubHelp();
		}
		return renderString(response, entity);
	}
	
	@RequestMapping(value="wxsave")
	public String wxsave(HttpServletRequest request, HttpServletResponse response,String title,String authorname,String article,String imgurl,String authorid,String destamt,String gotamt) {
		String id = UUID.randomUUID().toString().replace("-", "");
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
		String publishtime = df.format(new Date());
		int result = pubHelpService.wxsave(id,title,authorname,article,imgurl,authorid,publishtime,destamt,gotamt);
		
		return renderString(response, result);
	}

}